<?php

namespace app\admin\controller;
use app\common\controller\Adminbase;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

import('Curl', EXTEND_PATH);
import('Interfaces', EXTEND_PATH);
/**
 * 玩家管理控制器
 */
//class Player extends AdminBase {
 class Player extends controller{
	/**
     * 玩家管理
     * @return
     */
    public function Gameplayer(){
       return $this->fetch();
    }
    /**
     * 玩家管理 （历史记录）
     * @return
     */
    public function Gameplayer_1(){
       return $this->fetch();
    }
    /**
     * 玩家信息查看
     * @return
     */
    public function player_message(){
        //获取用户信息
        $uid = input("param.uid");
        $nickname = input("param.nickname");
        $message_url =USER_HTTP."api/get_batch_info.php";
        $player_id = "";
        $message_data = [
            'app_id'=>APP_ID,
            'uid'=>$uid,
            'nickname'=>$nickname
        ];
        $message_data = json_encode($message_data);
        $curl = new \Curl();
        $message_result = $curl->vpost($message_url,$message_data);
        $message_obj = json_decode($message_result);
        if(!empty($message_obj->data)){
            $player_id = $message_obj->data[0]->uid;
        }
    	// print_r($message_result);die;
        //获取用户资产
        $assets_url =USER_HTTP."api/get_player_property.php";
        $assets_data = [
            'app_id'=>APP_ID,
            'uid'=>$player_id,
            // 'property_type'=>$property_type
        ];
        $assets_data = json_encode($assets_data);
        $curl = new \Curl();
        $assets_result = $curl->vpost($assets_url,$assets_data);
        $assets_obj = json_decode($assets_result);
        $drill = [];
        $Gift_drill = 0;//赠送钻石
        $Purchase_drill = 0;//购买钻石
        foreach ($assets_obj->data as $key => $value) {
            array_push($drill,(array)$value);
        }
        foreach ($drill as $k=>$v){
            if($v['property_type'] == 10001){
                $Purchase_drill += $v['property_num'];
            }
            if($v['property_type'] == 10002){
                $Gift_drill += $v['property_num'];
            }
        }
//        print_r($drill);die;
        $recharge = Db::name('order')->where('player_id', $uid)->where("goods_type",10001)->sum('fee');//累计充值数
        $club_cdr_user_cash = Db::name('club_cdr_user')->field('club_name,')->where('player_id', $player_id)->sum('cash');//可提现金额
        $club_cdr_user_status = Db::name('blacklist')->field('banned_length,why,start_time')->where('player_id', $player_id)->find();//营销账号状态
        $startDate = $club_cdr_user_status['start_time'];
        $overDate = date('Y-m-d H:i:s',time());
        $hours= floor(strtotime($overDate)-strtotime($startDate))%86400/60/600;
        if(!empty($club_cdr_user_status)){
            if($club_cdr_user_status['banned_length'] > $hours){
                $remaining_time = ceil($club_cdr_user_status['banned_length'] - $hours);
            }else{
                $remaining_time = 0;
            }

            $message_obj->data[0]->state = 0;//营销账号状态
            $message_obj->data[0]->banned_length = $club_cdr_user_status['banned_length'];//封禁时长
            $message_obj->data[0]->why = $club_cdr_user_status['why'];//封禁原因
            $message_obj->data[0]->start_time = $club_cdr_user_status['start_time'];//封禁时间
            $message_obj->data[0]->remaining_time = $remaining_time;//封禁剩余时间
        }else{
            $message_obj->data[0]->state = 1;//营销账号状态
        }
		// print_r($message_obj->data[0]);die;
        // $start_time =   $message_obj->data[0]->seal_up_start_time;
       if($message_obj->data[0]->seal_up_duration !=0){
       	$freeze_hours =  ceil($message_obj->data[0]->seal_up_duration/3600);
       }else{
       		$freeze_hours = 0;
       }
    
        $users_id = Session::get('uid');
        $user_type = Db::name("users")->where("id",$users_id)->find();
        $auth_group = Db::name('auth_group')->where("id",$user_type['type'])->find();
        if($auth_group['title'] == '管理员'){
            $message_obj->data[0]->auth_group = 1 ;
        }elseif($auth_group['title'] == '高级运营'){
            $message_obj->data[0]->auth_group = 1 ;
        }else{
            $message_obj->data[0]->auth_group = 0 ;
        }
        
        
        $club = model('clubModel');
        $Balance= $club->Balance($uid);
        $message_obj->data[0]->Balance = $Balance;//
        $message_obj->data[0]->Gift_drill = $Gift_drill;//
        $message_obj->data[0]->Purchase_drill = $Purchase_drill;//
        $message_obj->data[0]->recharge = $recharge/100;//累计充值
        $message_obj->data[0]->withdrawal = $club_cdr_user_cash/100;//可提现金额
        $message_obj->data[0]->freeze_remaining_time = $freeze_hours ;//封禁剩余时间

        return [$message_obj->data[0]];
        // print_r($message_obj->data);

    }
    /**
     * 玩家金币数
     * @return
     */
    public function gold(){
        $player_id = input("param.id");
        $assets_url =USER_HTTP."api/get_player_property.php";
        $assets_data = [
            'app_id'=>APP_ID,
            'uid'=>$player_id,
        ];
        $assets_data = json_encode($assets_data);
        $curl = new \Curl();
        $assets_result = $curl->vpost($assets_url,$assets_data);
        $assets_obj = json_decode($assets_result);
        foreach ($assets_obj->data as $key => $value) {
            $str = $value->property_type;
            $club_id = explode('_', $str);
            $club = Db::name('club')
                    ->field('club_name')
                    ->where("cid",$club_id[0])
                    ->find();
            $value->club_name =  base64_decode($club['club_name']);
            $value->club_id = $club_id[0];
            $value->gold_num = $value->property_num+$value->freeze_num;
           
        }

        return json($assets_obj->data);
    }
    /**
     * 所属俱乐部
     * @return
     */
    public function affiliated_club(){
        $where = [];
        $wheres = [];
        $player_id = input("param.id");//主键id
        // $player_id = 827179;
        $club = Db::name('user_club')
                // ->Distinct(true)
                ->alias('uc')
                ->field('club_name,uc.create_time,uc.player_id,cid,club_status')
                ->join('tb_club c','uc.club_id= c.cid')
                ->where('uc.player_id', $player_id)
                ->where('uc.status', 1)
                ->where("c.club_status",1)
                ->select();//所属俱乐部
        foreach ($club as $key => $value) {
            $wheres['cid'] = $value['cid'];
            $club[$key]['club_name'] = base64_decode($value['club_name']);
            $wheres['president_id'] = $value['player_id'];
            $if_club = Db::name('club')
                    ->where($wheres)
                    ->where("club_status",1)
                    ->find();
            if($if_club){
                $club[$key]['President'] = 1;
            }else{
                 $club[$key]['President'] = 0;
            }
        }

        return json($club);
    }
     /**
     * 所属亲友圈
     * @return
     */
    public function affiliated_(){
        $where = [];
        $w = [];
        $player_id = input("param.id");//主键id
        // $player_id = 827179;
        $where['player_id'] =$player_id;
        $club = Db::name("user_club")
                ->where($where)
                ->select();
        foreach ($club as $k =>$v){
            $club_name = Db::name("club")
                    ->where("cid",$v['club_id'])
                    ->where("club_status",1)
                    ->find();
            $club[$k]['club_name'] = base64_decode($club_name['club_name']);
            $club[$k]['club_status'] = $club_name['club_status'];

            $w['uid'] = $v['player_id'];
            $w['club_id'] = $v['club_id'];
            $w['vip_status'] = 1;
            $vip_card = Db::name('user_vip')
                ->where($w)
                ->find();
//            print_r($w);
            if($vip_card){
                $vip_name = Db::name('vip_card')->where("vip_id",$vip_card['vid'])->find();
                $club[$k]['end_day'] = $vip_card['end_day'];
                $club[$k]['vip_name'] = $vip_name['name'];
            }else{
                $club[$k]['end_day'] = "";
                $club[$k]['vip_name'] ="";
            }
            if($club_name['president_id'] == $player_id){
                $club[$k]['President'] = 1;
            }else{
                $club[$k]['President'] = 0;
            }
        }
        foreach ($club as $k =>$v){
            if( $v['club_status'] != 1){
                unset($club[$k]);
            }
        }
//        print_r($club);
        return json($club);
    }
    /**
     * 营销账号 封禁
     * @return
     */
    public function banned(){
        $player_id = input("param.id");//主键id
        $banned_length = input("param.banned_length");//冻结时长
        $why = input("param.why");//封禁原因

        //操作记录 记录
        $time = date("Y-m-d H:i:s",time());//日期时间
        $operation = Session::get("username");//操作人
        $operation_object = input("param.operation_object").'/'.$player_id;//操作对象
        $type = 1;//操作类型 封禁
        $details = "封禁".$banned_length.'小时';
        $befor_fstatus = '解封';//操作前状态
        $operation_why = $why;//原因

        $club = Db::name('blacklist')
                ->where("player_id",$player_id)
                ->update([
//                    'auth_status' => 0,
                    'banned_length' => $banned_length,
                    'start_time' => date("Y-m-d H:i:s",time()),
                    'why' =>$why
                    ]);
        $club = Db::name('blacklist')
            ->insert([
                'player_id' =>$player_id,
                'banned_length' => $banned_length,
                'start_time' => date("Y-m-d H:i:s",time()),
                'why' =>$why
            ]);
        if($club){
            $player = model('playerModel');
            $player_perform = $player->record($time,$operation,$operation_object,$type,$details,$befor_fstatus,$operation_why);
            return json(['code' => 1, 'url' => '', 'msg' => '封禁成功']);
        }else{
            return json(['code' => 0, 'url' => '', 'msg' => '封禁失败']);
        }
    }
    /**
     * 营销账号 解封
     * @return
     */
    public function unlock(){
        $unlock_id = input("param.id");//主键id
        $unlock_type = input("param.unlock_type");//操作：
        $unlock_save_time = input("param.unlock_save_time");//封禁时长
        $unlock_reason = input("param.unlock_reason");//解封原因
        //操作记录 记录
        $time = date("Y-m-d H:i:s",time());//日期时间
        $operation = Session::get("username");//操作人
        $operation_object = input("param.operation_object").'/'.$unlock_id;//操作对象
        $type = 2;//操作类型
        $befor_fstatus = '封禁';//操作前状态
        $operation_why = $unlock_reason;//原因

        $data = [
            'unlock_reason' =>$unlock_save_time
        ];
        if($unlock_type == 0){
            $details = '解封';//操作详情
            $club = Db::name('blacklist')
                // ->fetchsql(true)//打印sql语句
                ->where("player_id",$unlock_id)
                ->delete();
        }else{
            $data['start_time'] =date("Y-m-d H:i:s",time());
            $data['banned_length'] =$unlock_save_time ;
            $details = '修改封禁时长为'.$unlock_save_time.'小时';//操作详情
            $club = Db::name('blacklist')
                // ->fetchsql(true)//打印sql语句
                ->where("player_id",$unlock_id)
                ->update($data);
        }

        if($club){
            $player = model('playerModel');
            $player_perform = $player->record($time,$operation,$operation_object,$type,$details,$befor_fstatus,$operation_why);
            return json(['code' => 1, 'url' => '', 'msg' => '解禁成功']);
        }else{
            return json(['code' => 0, 'url' => '', 'msg' => '解禁失败']);
        }
    }

    /**
     *操作记录
     * @return
     */
    public function operation_records(){
        $where= [];
        $start_time = input("param.start_time");
        $end_time = input("param.end_time");
        $type = input("param.type");
        if($start_time !="" && $end_time !=""){
            $where['time'] = array(array('Egt',$start_time),array('Elt',$end_time), 'and');
        }
        if($type != 0){
            $where['type'] =$type;
        }
        $result = Db::name('player_operation')
                 ->where($where)
//                ->where('type',$type)
//                ->whereTime("time",'between',[$start_time, $end_time])
                ->order("id desc")
                ->select();
         return json($result);

    }
    /**
     *冻结
     * @return
     */
    public function thaw(){

            $uid = input("param.uid");//逗号 分隔
            $app_id = input("param.app_id");//应用的app_id
            $player_status = input("param.player_status");
            $seal_up_duration = input("param.seal_up_duration");
            $annotate = input("param.annotate");//备注
            $start_time =date("Y-m-d H:i:s",time());
            // $end_time = date("Y-m-d H:i:s",strtotime("$start_time + $seal_up_duration hour"));
            $url = USER_HTTP.'api/seal_up.php';

            $time = date("Y-m-d H:i:s",time());//日期时间
            $operation = Session::get("username");//操作人
            $operation_object = input("param.operation_object").'/'.$uid;//操作对象
            $type = 3;//操作类型 冻结
            $details = "冻结".$seal_up_duration.'小时';
            $befor_fstatus = '解冻';//操作前状态
            $operation_why = $annotate;//原因

            $data = [
                'app_id'=>APP_ID,
                'uid'=>$uid,
                'player_status'=>$player_status,
                'seal_up_duration'=>$seal_up_duration,
                'seal_up_start_time'=>$start_time,
                'annotate'=>$annotate,
            ];
            $message_data = json_encode($data);
            // print_r($data);die;
            $curl = new \Curl();
            $message_result = $curl->vpost($url,$message_data);
            $message_obj = json_decode($message_result);
            $player = model('playerModel');
            $player_perform = $player->record($time,$operation,$operation_object,$type,$details,$befor_fstatus,$operation_why);
            return json($message_obj); 

    }
    /**
     *解冻
     * @return
     */
    public function freeze(){
            $uid = input("param.uid");//逗号 分隔
            $app_id = input("param.app_id");//应用的app_id
            $types = input("param.type");
            $seal_up_duration = input("param.seal_up_duration");
            $annotate = input("param.annotate");//备注
            $start_time =date("Y-m-d H:i:s",time());
            // $end_time = date("Y-m-d H:i:s",strtotime("$start_time + $seal_up_duration hour"));
            $url = USER_HTTP.'api/seal_up.php';

            $time = date("Y-m-d H:i:s",time());//日期时间
            $operation = Session::get("username");//操作人
            $operation_object = input("param.operation_object").'/'.$uid;//操作对象
            $type = 4;//操作类型 冻结
            $befor_fstatus = '冻结';//操作前状态
            $operation_why = $annotate;//原因

            $data = [
                'app_id'=>APP_ID,
                'uid'=>$uid,
                'seal_up_start_time'=>$start_time,
                // 'seal_up_end_time'=>$end_time,
                'annotate'=>$annotate,
            ];
            if($types == 0){
                $data['player_status'] = 1;
            }else{
                 $data['player_status'] = 0;
                 $data['seal_up_duration'] = $seal_up_duration;
            }
            if($type == 0){
                    $data['opera_type'] =4;
                    $details = '解冻';//操作详情
             }else{
                    $data['start_time'] =date("Y-m-d H:i:s",time());
                    $data['banned_length'] =$seal_up_duration ;
                    $details = '修改封禁时长为'.$seal_up_duration.'小时';//操作详情
            }
            $message_data = json_encode($data);
            $curl = new \Curl();
            $message_result = $curl->vpost($url,$message_data);
            $message_obj = json_decode($message_result);
            $player = model('playerModel');
            $player_perform = $player->record($time,$operation,$operation_object,$type,$details,$befor_fstatus,$operation_why);
            return json($message_obj);
            
            

    }
    /**
     *修改用户信息
     * @return
     */
    public function save_player_message(){
        $uid = input("param.id");
        $Purchase_drill = input("param.Purchase_drill");
        $Gift_drill = input("param.Gift_drill");

        $time = date("Y-m-d H:i:s",time());//日期时间
        $operation = Session::get("username");//操作人
        $operation_object = input("param.operation_object").'/'.$uid;//操作对象
        $type = 6;//操作类型 修改信息
        $operation_why ="";
        $upinfo = [];
        $befor_fstatus = "购买蓝钻：".$Purchase_drill.'|'."赠送蓝钻：".$Gift_drill;
        $details = "购买蓝钻：".$Purchase_drill.'|'."赠送蓝钻：".$Gift_drill;
        $Purchase_data = [
            'app_id'=>APP_ID,
            'uid'=>$uid,
            'property_type'=>10001,
            'property_num'=>$Purchase_drill
        ];
        $Gift_data = [
            'app_id'=>APP_ID,
            'uid'=>$uid,
            'property_type'=>10002,
            'property_num'=>$Gift_drill 
        ];
        array_push($upinfo,$Purchase_data);
        array_push($upinfo,$Gift_data);
            $gold_data = [
                'upinfo'=>$upinfo

            ];
            $gold_data = json_encode($gold_data);
//            print_r($gold_data);
//            die;
            $curl = new \Curl();
            $gold_url = USER_HTTP.'api/set_player_property_more.php';
            $result = $curl->vpost($gold_url,$gold_data);
            $obj = json_decode($result);
//            print_r($obj);die;
            if($obj->code == 0){
                 $player = model('playerModel');
                 $player_perform = $player->record($time,$operation,$operation_object,$type,$details,$befor_fstatus,$operation_why);
                 return json(['code' => 1, 'url' => '', 'msg' => '修改成功']);

            }else{
                 return json(['code' => 0, 'url' => '', 'msg' => '用户资产修改失败']);
            }

    }

    
}